package leetcode_901_1000;

import java.util.ArrayList;
import java.util.List;

public class LeeCode_989 {
    public static void main(String[] args) {
//        System.out.println(addToArrayForm(new int[]{2, 1, 5}, 806));
        System.out.println(addToArrayForm(new int[]{9,9,9}, 1));
    }
    private static List<Integer> addToArrayForm(int[] num, int k) {
        int n = num.length;
        String s = Integer.toString(k);
        int i = n - 1, j = s.length() - 1;
        int tmp = 0;
        StringBuilder sb = new StringBuilder();
        while (i >= 0 && j >= 0){
            int n1 = num[i], n2 = s.charAt(j) - '0';
            i--;
            j--;
            tmp += n1 + n2;
            sb.append(tmp % 10);
            tmp /= 10;
        }
        while (i >= 0){
            tmp += num[i];
            sb.append(tmp % 10);
            tmp /= 10;
            i--;
        }
        while (j >= 0){
            tmp += s.charAt(j) - '0';
            sb.append(tmp % 10);
            tmp /= 10;
            j--;
        }
        if (tmp > 0){
            sb.append(tmp);
        }
        sb.reverse();
        List<Integer> ans = new ArrayList<>();
        for (int l = 0; l < sb.length(); l++) {
            ans.add(sb.charAt(l) - '0');
        }
        return ans;
    }
}